<?

class Mdl_Posts extends MY_Model {

    function Mdl_Posts() {

        parent::MY_Model();

        $this->limit = 25;

    }

    function get($params = NULL) {

        if (isset($params['search_hash'])) {

            $this->db->where('hash', $params['search_hash']);

            $query = $this->db->get('inktype_hash');

            $result = $query->row();

            $this->db->where('MATCH (excerpt,extended) AGAINST ("' . $result->query_info . '" IN BOOLEAN MODE)', NULL, FALSE);

        }

        $this->db->select('*, (SELECT COUNT(*) FROM inktype_comments WHERE inktype_comments.post_id = inktype_posts.post_id AND is_spam = 0) AS num_comments');

        $this->prep_params($params);

        if (!isset($params['order_by'])) {

            $this->db->order_by('date DESC');

        }

        if (isset($params['url_title'])) {

            $this->db->where('url_title', $params['url_title']);

        }

        if (isset($params['post_id'])) {

            $this->db->where('post_id', $params['post_id']);

        }

        if ((!isset($params['url_title']) AND !isset($params['post_id']) AND !isset($params['limit'])) OR (isset($params['limit']) AND isset($params['paginate']) AND $params['paginate'] == TRUE)) {

            $offset = ($this->page * $this->limit) - $this->limit;

            $this->db->limit($this->limit, $offset);

        }

        if (isset($params['category_url_title'])) {

            $this->db->where('inktype_postcats.post_id = inktype_posts.post_id');

            $this->db->where('inktype_postcats.category_id = inktype_categories.category_id');

            $this->db->where('inktype_categories.url_title', $params['category_url_title']);

        }

        $this->db->join('inktype_users', 'inktype_users.user_id = inktype_posts.user_id');

        if (isset($this->is_admin)) {

            $this->db->where('inktype_posts.site_id', $this->session->userdata('site_id'));

            if ($this->session->userdata('user_type_id') <> 1 and $this->session->userdata('user_type_id') <> 2) {

                $this->db->where('inktype_posts.user_id', $this->session->userdata('user_id'));

            }

        }

        else {

            $this->db->where('inktype_posts.site_id', $this->inktype->site_id);

            $this->db->where('inktype_posts.published', 1);

        }

        if (isset($params['category_url_title'])) {

            $query = $this->db->get('inktype_posts, inktype_postcats, inktype_categories');

        }

        else {

            $query = $this->db->get('inktype_posts');

        }

        if ((!isset($params['url_title']) AND !isset($params['post_id']) AND !isset($params['limit'])) OR (isset($params['limit']) AND isset($params['paginate']) AND $params['paginate'] == TRUE)) {

            $this->set_page_links();

        }

        $posts = $query->result();

        foreach ($posts as $key=>$post) {

            unset($post->password, $post->last_login, $post->date_registered, $post->website_url, $post->category_id, $post->category);

            $this->db->where('post_id', $post->post_id);

            $this->db->join('inktype_categories', 'inktype_categories.category_id = inktype_postcats.category_id');

            $query = $this->db->get('inktype_postcats');

            foreach ($query->result() as $result) {

                $posts[$key]->category_id[] = $result->category_id;

                $posts[$key]->categories[] = array(
                    'category_id'   =>  $result->category_id,
                    'category'      =>  $result->category,
                    'url_title'     =>  $result->url_title
                );

            }

        }

        return $posts;

    }

    function get_by_category($url_title, $params = NULL) {

        $this->prep_params($params);

        $this->db->select('*,(SELECT COUNT(*) FROM inktype_comments WHERE inktype_comments.post_id = inktype_posts.post_id) AS num_comments');

        $this->db->join('inktype_postcats', 'inktype_postcats.category_id = inktype_categories.category_id');

        $this->db->join('inktype_posts', 'inktype_posts.post_id = inktype_postcats.post_id');

        $this->db->join('inktype_users', 'inktype_users.user_id = inktype_posts.user_id');

        $this->db->where('inktype_categories.url_title', $url_title);

        $this->db->order_by('inktype_posts.date DESC');

        if (isset($params['limit']) AND isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $offset = ($this->page * $this->limit) - $this->limit;

            $this->db->limit($this->limit, $offset);

        }

        $query = $this->db->get('inktype_categories');

        if (isset($params['limit']) AND isset($params['paginate']) AND $params['paginate'] == TRUE) {

            $this->set_page_links();

        }

        $posts = $query->result();

        foreach ($posts as $key=>$post) {

            unset($post->password, $post->last_login, $post->date_registered, $post->website_url, $post->category_id, $post->category);

            $this->db->where('post_id', $post->post_id);

            $this->db->join('inktype_categories', 'inktype_categories.category_id = inktype_postcats.category_id');

            $query = $this->db->get('inktype_postcats');

            foreach ($query->result() as $result) {

                $posts[$key]->category_id[] = $result->category_id;

                $posts[$key]->categories[] = array(
                    'category_id'   =>  $result->category_id,
                    'category'      =>  $result->category,
                    'url_title'     =>  $result->url_title
                );

            }

        }

        return $posts;

    }

    function get_num_posts() {

        $this->db->select('COUNT(*) AS num_posts');

        $this->db->where('site_id', $this->session->userdata('site_id'));

        $query = $this->db->get('inktype_posts');

        $result = $query->row();

        return $result->num_posts;

    }

    function save() {

        $db_array = $this->prep_db_array();

        $db_array['site_id'] = $this->session->userdata('site_id');

        $db_array['date'] = strtotime($this->input->post('year') . '/' . $this->input->post('month') . '/' . $this->input->post('day') . ' ' . $this->input->post('hour') . ':' . $this->input->post('minute'));

        $db_array['url_title'] = url_title($this->input->post('title'));

        unset($db_array['month'], $db_array['year'], $db_array['day'], $db_array['minute'], $db_array['hour'], $db_array['category_id']);

        if (!isset($db_array['is_sticky'])) {

            $db_array['is_sticky'] = 0;

        }

        if (!isset($db_array['allow_comments'])) {

            $db_array['allow_comments'] = 0;

        }

        if (!isset($db_array['published'])) {

            $db_array['published'] = 0;

        }

        if (isset($this->uri_assoc['post_id'])) {

            $this->db->where('post_id', $this->uri_assoc['post_id']);

            $this->db->update('inktype_posts', $db_array);

            $this->db->where('post_id', $this->uri_assoc['post_id']);

            $this->db->delete('inktype_postcats');

            foreach ($this->input->post('category_id') AS $category_id) {

                $db_array = array(
                    'post_id'       =>  $this->uri_assoc['post_id'],
                    'category_id'   =>  $category_id
                );

                $this->db->insert('inktype_postcats', $db_array);

            }

        }

        else {

            $this->db->insert('inktype_posts', $db_array);

            $post_id = $this->db->insert_id();

            foreach ($this->input->post('category_id') AS $category_id) {

                $db_array = array(
                    'post_id'       =>	$post_id,
                    'category_id'   =>	$category_id
                );

                $this->db->insert('inktype_postcats', $db_array);

            }

        }

        $this->session->set_flashdata('success_save', TRUE);

    }

    function delete() {

        if (isset($this->uri_assoc['post_id'])) {

            $post_id = $this->uri_assoc['post_id'];

            $this->db->where('post_id', $post_id);

            if ($this->session->userdata('user_type_id') == 3 OR $this->session->userdata('user_type_id') == 4) {

            // this is either a global author or a site author and should only be able
            // to delete their own posts

                $this->db->where('user_id', $this->session->userdata('user_id'));

            }

            $this->db->delete('inktype_posts');

            $this->session->set_flashdata('success_delete', TRUE);

        }

    }

    function validate() {

        $this->fields = array(
            'date'              =>  'Date',
            'month'             =>  'Month',
            'day'               =>  'Day',
            'year'              =>  'Year',
            'hour'              =>  'Hour',
            'minute'            =>  'Minute',
            'category_id'       =>  'Category',
            'user_id'           =>  'Author',
            'title'             =>  'Title',
            'excerpt'           =>  'Excerpt',
            'extended'          =>  'Extended',
            'published'         =>  'Published',
            'url_title'         =>  'URL Title',
            'is_sticky'         =>  'Make Sticky',
            'allow_comments'    =>  'Allow Comments'
        );

        $this->rules = array(
            'user_id'       =>  'required',
            'title'         =>  'required',
            'excerpt'       =>  'required',
            'category_id'   =>  'required',
            'month'         =>  'required',
            'day'           =>  'required',
            'year'          =>  'required',
            'hour'          =>  'required',
            'minute'        =>  'required'
        );

        return $this->_validate();

    }

}

?>